﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Configuration;
using System.Threading;
using PRET.ReportAPP.Forms;

namespace PRET.ReportAPP
{
    public partial class MainForm : Form
    {
        delegate void CloseFromCallback();

        private string selectedReportType = "";
        private WaitForm waitform;
        private BackgroundWorker backgroundWait;
        private Control currentView = null;

        public MainForm()
        {
            // 显示初始化窗口
            ShowInitializeWindow();

            InitializeComponent();
            
            // 初始化所有报表窗口
            InitializeAllReportView();

            // 关闭初始化窗口
            CloseInitializeWindow();
        }

        private void InitializeAllReportView()
        {
            // 初始化挤出机报表
            this.productionReportView.SetOrderList();

            // 初始化均化机报表
            this.JHJReportViewer.SetOrderList();

            // 初始化电表单价数值
            this.meterReadingReportView.SetUnitPrice();

            // 初始化高低柜设备列表
            this.highLowReportView.SetDeviceList();

            // 初始化生产线效率报表
            this.productionLineEffReportView.SetEfficiencyQuota();

            // 初始化班组效率报表
            this.teamEffReportView.SetEfficiencyQuota();
        }

        #region 显示初始化窗口
        private void ShowInitializeWindow()
        {
            // 显示等待窗口
            this.waitform = new WaitForm();
            backgroundWait = new BackgroundWorker();
            this.backgroundWait.DoWork += showWaitForm;
            this.backgroundWait.RunWorkerAsync(this.waitform);
        }

        // 显示等待窗口
        private void showWaitForm(object sender, DoWorkEventArgs e)
        {
            ((WaitForm)e.Argument).ShowDialog();
        }

        // 关闭等待窗口
        private void CloseInitializeWindow()
        {
            if (this.waitform.InvokeRequired)
            {
                CloseFromCallback d = new CloseFromCallback(CloseInitializeWindow);
                this.waitform.Invoke(d);
            }
            else
            {
                this.waitform.CanClose = true;
                this.waitform.Close();
            }
        }
        #endregion

        private void InitializeReportViewerPanel()
        {
            InitializeReportSelectionCombox();
        }

        private void InitializeReportSelectionCombox()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>{
                {"productionReport", "生产报表"},
                {"JHJReport", "干燥均化机监控报表"},
                {"productiveTaskReport", "生产任务信息"},
                {"prolineStandbyReport", "生产线待机监控报表"},
                {"productionLineEfficiencyReport", "生产线效率数据"},
                {"teamEfficiencyReport", "班组生产效率数据"},
                {"meterReadingReport", "抄表系统报表"},
                {"highLowVoltageReport", "高低压柜电度报表"},
                {"highLowStatusReport", "高低柜参数记录表"},
                //{"deviceRunReport", "设备运行记录表"},
                {"hsmlReport", "滑石母粒机监控报表"},
                {"tvControl", "车间看板信息设置"}
            };
            this.reportSelectionCombox.DataSource = new BindingSource(dic, null);
            this.reportSelectionCombox.ValueMember = "Key";
            this.reportSelectionCombox.DisplayMember = "Value";
            this.reportSelectionCombox.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        #region 窗体事件
        private void ReportForm_Load(object sender, EventArgs e)
        {
            InitializeReportSelectionCombox();
        }

        private void ReportForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否确定退出程序？", "确认", MessageBoxButtons.YesNo) == DialogResult.No)
                e.Cancel = true;
        }

        private void reportSelectionCombox_SelectedIndexChanged(object sender, EventArgs e)
        {
            KeyValuePair<string, string> selectItem = (KeyValuePair<string, string>)this.reportSelectionCombox.SelectedItem;
            if (this.selectedReportType == selectItem.Key)
                return;
            else
            {
                hideCurrentReportViewer();

                this.reportViewGroup.Text = selectItem.Value;
                this.selectedReportType = selectItem.Key;
                switch (this.selectedReportType)
                {
                    case "deviceRunReport":
                        break;
                    case "tvControl":
                        this.reportViewGroup.Controls.Add(this.tvControlView);
                        currentView = this.tvControlView;
                        break;
                    case "highLowStatusReport":
                        // 高低柜运行参数
                        this.reportViewGroup.Controls.Add(this.highLowReportView);
                        currentView = this.highLowReportView;
                        break;
                    case "highLowVoltageReport":
                        // 高低压柜电度报表
                        this.reportViewGroup.Controls.Add(this.highLowVoltageReportView);
                        currentView = this.highLowVoltageReportView;
                        break;
                    case "prolineStandbyReport":
                        this.reportViewGroup.Controls.Add(this.standbyReportView);
                        currentView = this.standbyReportView;
                        break;
                    case "productionLineEfficiencyReport":
                        // 生产线效率报表
                        this.reportViewGroup.Controls.Add(this.productionLineEffReportView);
                        currentView = this.productionLineEffReportView;
                        break;
                    case "productiveTaskReport":
                        // 生产任务报表
                        this.reportViewGroup.Controls.Add(this.productiveTaskReportView);
                        currentView = this.productiveTaskReportView;
                        break;
                    case "hsmlReport":
                        this.reportViewGroup.Controls.Add(this.hsmlReportView);
                        currentView = this.hsmlReportView;
                        break;
                    case "teamEfficiencyReport":
                        // 班组生产效率报表
                        this.reportViewGroup.Controls.Add(this.teamEffReportView);
                        currentView = this.teamEffReportView;
                        break;
                    case "JHJReport":
                        // 显示干燥均化机报表
                        this.reportViewGroup.Controls.Add(this.JHJReportViewer);
                        currentView = this.JHJReportViewer;
                        break;
                    case "productionReport":
                        // 显示生产产品主报表
                        this.reportViewGroup.Controls.Add(this.productionReportView);
                        currentView = this.productionReportView;
                        break;
                    case "meterReadingReport":
                        // 显示电表报表
                        this.reportViewGroup.Controls.Add(this.meterReadingReportView);
                        currentView = this.meterReadingReportView;
                        break;
                    default:
                        break;
                }
            }
        }
        #endregion

        private void hideCurrentReportViewer()
        {
            if (currentView != null)
                this.reportViewGroup.Controls.Remove(currentView);
        }
    }
}
